//
//  ESCardWeatherDetailView.m
//  EasyStyle
//
//  Created by FK on 16/5/14.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESCardWeatherDetailView.h"
#import "ESWeatherNetResult.h"
#import "Masonry.h"

@interface ESCardWeatherDetailView ()
@property (weak, nonatomic) IBOutlet UIButton *closeWeatherDetialBtn;
@property (weak, nonatomic) IBOutlet UIButton *selectCity;
@property (weak, nonatomic) IBOutlet UIImageView *airQualityLabel;
@property (weak, nonatomic) IBOutlet UILabel *weatherInfoLabel;
@property (weak, nonatomic) IBOutlet UILabel *curPMLabel;
@property (weak, nonatomic) IBOutlet UIImageView *weatherImg;


@property (weak, nonatomic) IBOutlet UILabel *temperatureLabel;



//@property (nonatomic, strong)NSArray *weatherDetailList;
@property (weak, nonatomic) IBOutlet UIView *firstDayWeatherDetailView;
@property (weak, nonatomic) IBOutlet UIView *secondDayWeatherDetailView;
@property (weak, nonatomic) IBOutlet UIView *thirdDayWeatherDetailView;
@property (weak, nonatomic) IBOutlet UIView *fourthDayWeatherDetailView;
@property (weak, nonatomic) IBOutlet UIView *fifthDayWeatherDetailView;

@property (nonatomic, strong)NSArray *weatherDetailList;
@property (nonatomic, strong)NSArray *singleDayWeatherObjects;
@property (nonatomic, strong)NSArray *weatherBGViewObjects;
@end

@implementation ESCardWeatherDetailView


- (void)awakeFromNib
{
    
    
    [_selectCity addTarget:self action:@selector(selectCityClick:) forControlEvents:UIControlEventTouchUpInside];
    [_closeWeatherDetialBtn addTarget:self action:@selector(closeWeatherDetial:) forControlEvents:UIControlEventTouchUpInside];
    
    ESSingleDayWeatherView *firstDayView = [ESSingleDayWeatherView loadViewFromSameXibName];
    ESSingleDayWeatherView *secondDayView = [ESSingleDayWeatherView loadViewFromSameXibName];
    ESSingleDayWeatherView *thirdDayView = [ESSingleDayWeatherView loadViewFromSameXibName];
    ESSingleDayWeatherView *fourthDayView = [ESSingleDayWeatherView loadViewFromSameXibName];
    ESSingleDayWeatherView *fifthDayView = [ESSingleDayWeatherView loadViewFromSameXibName];
    
    _singleDayWeatherObjects = @[firstDayView,secondDayView,thirdDayView,fourthDayView,fifthDayView];
    
    _weatherBGViewObjects = @[_firstDayWeatherDetailView,_secondDayWeatherDetailView,_thirdDayWeatherDetailView,_fourthDayWeatherDetailView,_fifthDayWeatherDetailView];
}

- (void)loadCardWithData:(NSDictionary *)dictResultWeather
{
    
    
    ESWeatherNetResult *result = [ESWeatherNetResult new];
    [result getDetailWeatherData:dictResultWeather andDataType:nil];
    
    [_selectCity setTitle:result.realtime.cityName forState:UIControlStateNormal];
    _weatherInfoLabel.text = result.realtime.info;
    _temperatureLabel.text = [NSString stringWithFormat:@"%@%@",result.realtime.temperature,@"℃"];
    
    _weatherImg.image = [UIImage imageNamed:[weatherInfoImageDict objectForKey:result.realtime.img]];
    _curPMLabel.text = result.PM25.curPm;
    if (result.PM25.curPm.length <= 0) {
        _curPMLabel.text = result.PM25.pm25;
    }
    _airQualityLabel.image = [UIImage imageNamed:result.PM25.quality];
    
    _weatherDetailList = result.oneWeekWeather;
    
    for (NSInteger index = 0;index < 5 ;index++) {
        [self setupSingleDayWeatherViewWithIndex:index];
    }
}

- (void)setupSingleDayWeatherViewWithIndex:(NSInteger)arrIndex
{
    UIView *bgView = _weatherBGViewObjects[arrIndex];
    ESSingleDayWeatherView *singleDayView = _singleDayWeatherObjects[arrIndex];
    
    NSDictionary *dict = _weatherDetailList[arrIndex];;
    WeatherOneWeekInfo *dayWeatherInfo = dict[@"day"];
    singleDayView.dateLabel.text = [dayWeatherInfo.weatherDate substringFromIndex:5];
    singleDayView.temperatureLabel.text = [NSString stringWithFormat:@"%@%@",dayWeatherInfo.weathertemperature,@"℃"];
    singleDayView.weatherImage.image = [UIImage imageNamed:[weatherInfoImageDict objectForKey:dayWeatherInfo.weatherImgID]];
    
    [bgView addSubview:singleDayView];
    [singleDayView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(singleDayView.superview).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)closeWeatherDetial:(UIButton *)sender
{
    if (_blockCloseWeatherDetail) {
        _blockCloseWeatherDetail();
    }
}

- (void)selectCityClick:(UIButton *)sender
{
    if (_blockSelectCity) {
        _blockSelectCity();
    }
}


@end
